package com.sansheng.util;

import com.sansheng.entity.ChannelInfo;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Repository;
import java.io.IOException;
import java.util.Date;

/**
 * 网易公共方法
 * @author chenhui
 */
@Repository("SSHttpRequestManger")
public class SSHttpRequestManger {
    public String channelMethod(ChannelInfo channelInfo) throws IOException {

        DefaultHttpClient httpClient = new DefaultHttpClient();

        String url = "https://vcloud.163.com/app/channel/create";
        HttpPost httpPost = new HttpPost(url);

        String appKey = "f56158399fc03a62afa3fda8588c494c";
        String appSecret = "5c35af8c0ed9";
        String nonce = "1";
        String curTime = String.valueOf((new Date()).getTime() / 1000L);
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);//参考 计算CheckSum的java代码

        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/json;charset=utf-8");

        // 设置请求的参数
        StringEntity params = new StringEntity("{\"name\":\""+channelInfo.getName()+"\", \"type\":"+channelInfo.getType()+"}", Consts.UTF_8);
        httpPost.setEntity(params);

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);

        channelInfo.setNetease(EntityUtils.toString(response.getEntity(), "utf-8"));
        //return EntityUtils.toString(response.getEntity(), "utf-8");

        return channelInfo.toString();

    }

}